import 'package:flutter/material.dart';

// 自定义路由动画基类
class CustomPageRoute<T> extends PageRouteBuilder<T> {
  final Widget page;
  final Widget Function(
    BuildContext,
    Animation<double>,
    Animation<double>,
    Widget,
  )? transitionBuilder;
  
  CustomPageRoute({
    required this.page,
    RouteSettings? settings,
    this.transitionBuilder,
    Duration? transitionDuration,
  }) : super(
          pageBuilder: (context, animation, secondaryAnimation) => page,
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            return transitionBuilder?.call(
                    context, animation, secondaryAnimation, child) ??
                child;
          },
          transitionDuration: transitionDuration ?? const Duration(milliseconds: 300),
          settings: settings,
        );
}

// 1. 从右向左滑动
class SlideRightRoute<T> extends CustomPageRoute<T> {
  SlideRightRoute({required Widget page, RouteSettings? settings})
      : super(
          page: page,
          settings: settings,
          transitionBuilder: (context, animation, secondaryAnimation, child) {
            const begin = Offset(1.0, 0.0);
            const end = Offset.zero;
            const curve = Curves.easeInOut;
            
            var tween = Tween(begin: begin, end: end)
                .chain(CurveTween(curve: curve));
            
            return SlideTransition(
              position: animation.drive(tween),
              child: child,
            );
          },
        );
}

// 2. 淡入淡出
class FadeRoute<T> extends CustomPageRoute<T> {
  FadeRoute({required Widget page, RouteSettings? settings})
      : super(
          page: page,
          settings: settings,
          transitionBuilder: (context, animation, secondaryAnimation, child) {
            return FadeTransition(
              opacity: animation,
              child: child,
            );
          },
        );
}

// 3. 缩放
class ScaleRoute<T> extends CustomPageRoute<T> {
  ScaleRoute({required Widget page, RouteSettings? settings})
      : super(
          page: page,
          settings: settings,
          transitionBuilder: (context, animation, secondaryAnimation, child) {
            return ScaleTransition(
              scale: animation,
              child: child,
            );
          },
        );
}

// 4. 旋转加缩放
class RotateScaleRoute<T> extends CustomPageRoute<T> {
  RotateScaleRoute({required Widget page, RouteSettings? settings})
      : super(
          page: page,
          settings: settings,
          transitionBuilder: (context, animation, secondaryAnimation, child) {
            return RotationTransition(
              turns: Tween(begin: 0.0, end: 1.0)
                  .animate(
                    CurvedAnimation(
                      parent: animation,
                      curve: const Interval(0.0, 0.5, curve: Curves.easeOut),
                    ),
                  ),
              child: ScaleTransition(
                scale: Tween(begin: 0.0, end: 1.0)
                    .animate(
                      CurvedAnimation(
                        parent: animation,
                        curve: const Interval(0.5, 1.0, curve: Curves.easeOut),
                      ),
                    ),
                child: child,
              ),
            );
          },
          transitionDuration: const Duration(milliseconds: 600),
        );
}

// 5. 滑动渐变
class SlideUpFadeRoute<T> extends CustomPageRoute<T> {
  SlideUpFadeRoute({required Widget page, RouteSettings? settings})
      : super(
          page: page,
          settings: settings,
          transitionBuilder: (context, animation, secondaryAnimation, child) {
            return SlideTransition(
              position: Tween<Offset>(
                begin: const Offset(0.0, 0.5),
                end: Offset.zero,
              ).animate(animation),
              child: FadeTransition(
                opacity: animation,
                child: child,
              ),
            );
          },
        );
} 